package models

import (
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"godemo/common/helper"
	"net/http"
)

//ConfigJsonBody json request body.
type ConfigJsonBody struct {
	Id              string
	CaptchaType     string
	VerifyValue     string
	ConfigAudio     base64Captcha.ConfigAudio
	ConfigCharacter base64Captcha.ConfigCharacter
	ConfigDigit     base64Captcha.ConfigDigit
}

// base64Captcha create http handler
func GenerateCaptchaHandler(c *gin.Context) {

	var postParameters ConfigJsonBody
	postParameters.Id = helper.RandomStringNum()
	postParameters.CaptchaType = "image"
	postParameters.ConfigDigit.Width = 100
	postParameters.ConfigDigit.Height = 33
	postParameters.ConfigDigit.CaptchaLen = 3
	postParameters.ConfigDigit.MaxSkew = 3
	postParameters.ConfigDigit.DotCount = 10

	captchaId, captcaInterfaceInstance := base64Captcha.GenerateCaptcha(postParameters.Id, postParameters.ConfigDigit)
	base64blob := base64Captcha.CaptchaWriteToBase64Encoding(captcaInterfaceInstance)

	body := map[string]interface{}{"data": base64blob, "captchaId": captchaId}
	c.JSON(200, body)
}

// base64Captcha verify http handler
func CaptchaVerifyHandle(c *gin.Context) {

	c.Request.ParseForm()
	//比较图像验证码
	verifyResult := base64Captcha.VerifyCaptcha(c.Request.Form.Get("id"), c.Request.Form.Get("code"))
	c.JSON(http.StatusOK, gin.H{"res": verifyResult})

}
